import call_service from '@dfeidao/atom-nodejs/msg/call-service';
interface Message {
	usercode: string;
	userpw: string;
	headers: {
		cookie: string;
	};
}
export default async function atom(msg: Message, action_id: string, session_id: string) {
	const usercode = msg.usercode;
	const userpw = msg.userpw;
	// 1. 判断验证码是否正确
	// const res = await check(msg.body.captcha, msg.cookie.uk);
	// if (res) {
	// 通过通用的call_service接口, 调用java登录接口
	// 一般java提供的登录接口都会返回sessionID
	const result = await call_service<{ sessionID: string }>(action_id, session_id, 'loginservice.userlogin', { logintypenew: 'usercode', usercode, userpw });
	// 处理原有cookie
	const cookie = msg.headers.cookie;
	const c = msg.headers.cookie ? cookie.split(/\s*;\s*/).reduce((pre, cur) => {
		const [key, val] = cur.split('=');
		pre[key] = val;
		return pre;
	}, {}) : {};

	const obj = {
		data: result,
		cookie: {
			...c,
			sessionid: result.sessionID
		}
	};
	return obj;
	// } else {
	// 	return {
	// 		data: '验证码错误'
	// 	};
	// }
}
